/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.interceptors;

import com.lehe.common.Contants;
import com.lehe.common.exceptions.LHAdminException;
import com.lehe.common.exceptions.LHException;
import com.lehe.common.utils.ExpireDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录验证拦截器<br>
 *
 * @author 王丹
 * @since  2017-02-26<br>
 */

@Component(value = "authenticationInterceptor")
public class AuthenticationInterceptor extends HandlerInterceptorAdapter {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${config.copyright}")
    private String copyright;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object o = session.getAttribute(Contants.SESSION_USER_KEY);
        if(null == o){
            throw new LHException("请从乐和信息公众号进入");
        }
        ExpireDate expireDate = ExpireDate.valueof(copyright);
        if (expireDate.isExpired()) {
            throw new LHException("此软件使用版权已过期，请联系作者。");
        }
        return super.preHandle(request, response, handler);
    }
}
